package cn.edu.zut.cs.controller;


import cn.edu.zut.cs.mapper.EquipmentMapper;
import cn.edu.zut.cs.pojo.Equipment;
import cn.edu.zut.cs.pojo.User;
import cn.edu.zut.cs.pojo.Weighing;
import cn.edu.zut.cs.service.EquipmentService;
import cn.edu.zut.cs.service.WeighingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 设备管理 前端控制器
 * </p>
 *
 * @author equipment
 * @since 2021-06-16
 */
@Controller

public class EquipmentController {
    @Resource
    private EquipmentService equipmentService;
    @Resource
    private EquipmentMapper equipmentMapper;
    @Resource
    private WeighingService weighingService;


    //添加电子秤
    @PostMapping("/addequipment")
    public String addequipment(HttpSession session,Equipment equipment,@RequestParam(value = "equlocation")String equlocation,@RequestParam(value = "equid")String equid,@RequestParam(value = "equadmin")String equadmin,Model model){
        User loginUser = (User) session.getAttribute("loginUser");
        if(loginUser.getUserlevel()==1){
            QueryWrapper<Equipment> wrapper=new QueryWrapper<>();
            wrapper.eq("equid",equid).eq("equcompany",loginUser.getUsercompany());
            Equipment serviceOne = equipmentService.getOne(wrapper);
            if(serviceOne==null){
                equipment.setEquid(equid);
                equipment.setEqucompany(loginUser.getUsercompany()); 
                equipment.setEqulocation(equlocation);
                equipment.setEquadmin(equadmin);
                equipmentService.save(equipment);
                model.addAttribute("msg","添加成功！");
            }else{
                model.addAttribute("msg","设备已存在，添加失败！");
            }
        }else{
            model.addAttribute("msg","对不起，您无权添加");
        }
        return "equipment_add";
    }




    //修改电子秤信息
    @GetMapping("/equipment_modify/{id}")
    public String updatePro(HttpSession session,@PathVariable("id")String equid, Model model){
        User loginUser = (User) session.getAttribute("loginUser");
        Equipment equipment = equipmentService.selectEquiment(loginUser, equid);
        model.addAttribute("equipment",equipment);
        return "equipment_modify";

    }

    //修改电子秤信息
    @PostMapping("/equipment_modify/{id}")
    public String updateProduct(HttpSession session,@PathVariable("id") String equid,Equipment equipment,Model model){
        User loginUser = (User) session.getAttribute("loginUser");
        if(loginUser.getUserlevel()==1){
            equipmentService.updateEquiment(equipment);
            model.addAttribute("msg","修改成功！");
        }else{
            model.addAttribute("msg","对不起，您无权修改");
        }
        return "redirect:/equipment_modify/{id}";
    }

    //删除电子秤
    @GetMapping("/delequ/{id}")
    public String delequ1(HttpSession session,@PathVariable("id") String equid,@RequestParam(value = "pn",defaultValue = "1")Integer pn,Model model){
        User loginUser = (User) session.getAttribute("loginUser");
        Page<Equipment> Tpage = new Page<>(pn,6);
        QueryWrapper<Equipment> wrapper=new QueryWrapper<>();
        if(loginUser.getUserlevel()==1){
            equipmentService.deleteEquiment(equid);
            wrapper.eq("equcompany",loginUser.getUsercompany());
            Page<Equipment> page = equipmentService.page(Tpage, wrapper);
            List<Equipment> records = page.getRecords();
            model.addAttribute("equipment",page);
            model.addAttribute("msg1","成功删除");
            return "equipment";
        }else{
            wrapper.eq("equcompany",loginUser.getUsercompany()).eq("equlocation",loginUser.getUserlocation());
            Page<Equipment> page = equipmentService.page(Tpage, wrapper);
            List<Equipment> records = page.getRecords();
            model.addAttribute("equipment",page);
            model.addAttribute("msg1","对不起，您无权删除");
            return "equipment";
        }

    }


    //查询某个电子秤信息
    @RequestMapping("/equipmentlist/sea")
    public String selectOneEqu(HttpSession session,@RequestParam(value="searchid",required = false,defaultValue ="-1" )String searchid, @RequestParam(value ="searchloc",required = false,defaultValue ="-1")String searchloc, @RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model) {
        User loginUser = (User) session.getAttribute("loginUser");
        Page<Equipment> Tpage = new Page<>(pn, 6);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        if(loginUser.getUserlevel()==1){
            if (searchid.equals("-1") && searchloc.equals("-1")) {
                queryWrapper.eq("equcompany",loginUser.getUsercompany());
                Page<Equipment> page = equipmentService.page(Tpage, queryWrapper);
                List<Equipment> records = page.getRecords();
                model.addAttribute("equipment", page);
                model.addAttribute("searchid", searchid);
                model.addAttribute("searchloc", searchloc);
                return "equipment";
            } else if (!searchid.equals("-1")&&searchloc.equals("-1")) {
                queryWrapper.eq("equid", searchid).eq("equcompany",loginUser.getUsercompany());
                Page<Equipment> page = equipmentService.page(Tpage, queryWrapper);
                model.addAttribute("equipment", page);
                model.addAttribute("searchid", searchid);
                model.addAttribute("searchloc", searchloc);
                return "equipment1";
            } else if(searchid.equals("-1")&&!searchloc.equals("-1")){
                queryWrapper.like("equlocation", searchloc).eq("equcompany",loginUser.getUsercompany());
                Page<Equipment> page = equipmentService.page(Tpage, queryWrapper);
                model.addAttribute("equipment", page);
                model.addAttribute("searchid", searchid);
                model.addAttribute("searchloc", searchloc);
                return "equipment2";
            }else{
                queryWrapper.like("equlocation", searchloc)
                            .eq("equcompany",loginUser.getUsercompany())
                            .eq("equid",searchid);
                Page<Equipment> page = equipmentService.page(Tpage, queryWrapper);
                model.addAttribute("equipment", page);
                model.addAttribute("searchid", searchid);
                model.addAttribute("searchloc", searchloc);
                return "equipment2";
            }
        }else{
            if (searchloc.equals(loginUser.getUserlocation())&&searchid.equals("-1")) {
                queryWrapper.eq("equlocation",loginUser.getUserlocation())
                            .eq("equcompany",loginUser.getUsercompany());
                Page<Equipment> page = equipmentService.page(Tpage, queryWrapper);
                model.addAttribute("equipment", page);
                model.addAttribute("searchid", searchid);
                model.addAttribute("searchloc", searchloc);
                return "equipment1";
            } else if(searchloc.equals(loginUser.getUserlocation())&&!searchid.equals("-1")){
                queryWrapper.eq("equid", searchid)
                            .eq("equlocation",loginUser.getUserlocation())
                            .eq("equcompany",loginUser.getUsercompany());
                Page<Equipment> page = equipmentService.page(Tpage, queryWrapper);
                model.addAttribute("equipment", page);
                model.addAttribute("searchid", searchid);
                model.addAttribute("searchloc", searchloc);
                return "equipment1";
            }else{
                queryWrapper.eq("equlocation",loginUser.getUserlocation())
                            .eq("equcompany",loginUser.getUsercompany());
                Page<Equipment> page = equipmentService.page(Tpage, queryWrapper);
                model.addAttribute("equipment", page);
                model.addAttribute("searchid", searchid);
                model.addAttribute("searchloc", searchloc);
                return "equipment";
            }
        }

    }

    //展示每台设备称重比率和总金额比率
    @RequestMapping("/weightinglist/ratio")
    @Transactional
    @Rollback(false)
    public String selectWeightRatio(HttpSession session,@RequestParam(value ="equlocation",required = false,defaultValue = "-1")String equlocation,@RequestParam(value ="weighttime1",required = false,defaultValue = "2001-06-19 16:16:12")String date1, @RequestParam(value ="weighttime2",required = false,defaultValue = "4000-06-19 16:16:12")String date2,@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        User loginUser = (User) session.getAttribute("loginUser");
        Date date=null;
        Date date0=null;
        Date datee=null;
        Date datee1=null;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = dateFormat.parse(date1);
            date0 = dateFormat.parse(date2);
            datee = dateFormat.parse("2001-06-19 16:16:12");
            datee1 = dateFormat.parse("4000-06-19 16:16:12");

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c=Calendar.getInstance();
        Calendar c1=Calendar.getInstance();

        c.setTime(date);
        c1.setTime(date0);

        equipmentService.countAllMoney(loginUser,date,date0);
        Page<Equipment> Tpage = new Page<>(pn,6);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        if(!date1.equals("2001-06-19 16:16:12")&&!date2.equals("4000-06-19 16:16:12")){
            equipmentService.countAllMoney(loginUser,date,date0);
        }else{
            equipmentService.countAllMoney(loginUser,datee,datee1);
        }
        if(loginUser.getUserlevel()==1){
            if(equlocation.equals("-1")){
                queryWrapper.eq("equcompany",loginUser.getUsercompany());
                Page<Equipment> page = equipmentService.page(Tpage, queryWrapper);
                List<Equipment> records = page.getRecords();
                model.addAttribute("equipment",page);
                model.addAttribute("equlocation",equlocation);
                return "home_panel";
            }else{
                queryWrapper.eq("equlocation",equlocation).eq("equcompany",loginUser.getUsercompany());
                Page<Equipment> page = equipmentService.page(Tpage, queryWrapper);
                List<Equipment> records = page.getRecords();
                model.addAttribute("equipment",page);
                model.addAttribute("equlocation",equlocation);
                return "home_panel1";
            }
        }else{
            if(equlocation.equals("-1")){
                queryWrapper.eq("equcompany",loginUser.getUsercompany()).eq("equlocation",loginUser.getUserlocation());
                Page<Equipment> page = equipmentService.page(Tpage, queryWrapper);
                List<Equipment> records = page.getRecords();
                model.addAttribute("equipment",page);
                model.addAttribute("equlocation",equlocation);
                return "home_panel";
            }else{
                queryWrapper.eq("equlocation",equlocation).eq("equcompany",loginUser.getUsercompany()).eq("equlocation",loginUser.getUserlocation());
                Page<Equipment> page = equipmentService.page(Tpage, queryWrapper);
                List<Equipment> records = page.getRecords();
                model.addAttribute("equipment",page);
                model.addAttribute("equlocation",equlocation);
                return "home_panel1";
            }
        }

    }
}

